package com.cskaoyan.controller.stage;

import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.ExceptionVO;
import com.cskaoyan.model.vo.WxSearchIndexDefaultVO;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.WxKeyWordService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

/**
 * @ClassName: WxSearchController
 * @Description: WxSearchController层
 * @Author:YPS
 * @Date: 2022/5/8 21:57
 * @Version: 1.0
 **/
@RestController
@RequestMapping("wx/search")
public class WxSearchController {

    @Autowired
    WxKeyWordService keyWordService;
    @Autowired
    UserService userService;
    @RequestMapping("index")
    public BaseRespVO<WxSearchIndexDefaultVO> getDefaultIndex(){
        WxSearchIndexDefaultVO search = keyWordService.getDefaultIndex();
        return BaseRespVO.ok(search);
    }

    @RequestMapping("helper")
    public BaseRespVO<List<String>> getSearchHistory(String keyword){
        List<String> historyList = keyWordService.getHistoryList(keyword);
        return BaseRespVO.ok(historyList);
    }
    @RequestMapping("clearhistory")
    public ExceptionVO clearHistory(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = userService.getUserIdByUsername(username);
        keyWordService.deleteHistory(userId);
        return new ExceptionVO();
    }

}
